package com.tanhua.server.controller;

import com.tanhua.model.db.User;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.intercepror.UserHolder;
import com.tanhua.server.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    /**
     * 查询用户资料
     *   get    /users?userID=123
     *   请求头
     *      Authorization
     *   Query参数
     *      userID ：当此ID存在根据此id查询用户信息，如果不存在，解析token获取当前用户id
     *   响应UserInfo对象
     */
    @GetMapping
    public ResponseEntity users(Long userID) {

        //1、请求参数处理
        if(userID == null) {
            User user = UserHolder.getUser();
            userID = UserHolder.getUserId();
        }
        //2、调用service
        UserInfoVo info = usersService.findUserInfoById(userID);
        //3、构造返回
        return ResponseEntity.ok(info);
    }

    /**
     * 更新用户资料
     *   PUT
     *   bady参数：
     *      UserInfo
     *   返回值：null
     */
    @PutMapping
    public ResponseEntity updataUserInfo(@RequestBody UserInfo userInfo) {
        //1、处理请求参数
        userInfo.setId(UserHolder.getUserId());
        //2、调用service
        usersService.updateUserInfo(userInfo);
        //3、返回
        return ResponseEntity.ok(null);
    }

    /**
     * 查询用户通用设置
     *   get  -  /settings
     *   响应数据：SettingsVo
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
        //调用service
        SettingsVo vo = usersService.settings();
        //返回
        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题
     *    POST   /questions
     *    请求参数：
     *      Body：
     *         content
     *    不需要返回值：null
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        //1、解析请求
        String content = map.get("content").toString();
        //2、调用service
        usersService.questions(content);
        //3、返回
        return ResponseEntity.ok(null);
    }

    /**
     * 设置通知开关
     *   POST    /notifications/setting
     *   请求参数：Body
     *      likeNotification
     *      pinglunNotification
     *      gonggaoNotification
     *   不需要返回值：null
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity setting(@RequestBody Map map) {
        //1、解析请求参数
        String like = map.get("likeNotification").toString();
        String pinglun = map.get("pinglunNotification").toString();
        String gonggao =  map.get("gonggaoNotification").toString();
        Boolean likeNotifi = Boolean.valueOf(like);
        Boolean pinglunNotifi = Boolean.valueOf(pinglun);
        Boolean gonggaoNotifi = Boolean.valueOf(gonggao);
        //2、调用service
        usersService.setting(likeNotifi,pinglunNotifi,gonggaoNotifi);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询黑名单分页列表
     *   GET    /blacklist？page=1&pagesize=5
     *   参数（Query）
     *      page，pagesize
     *   返回值：
     *      PageResult
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        //1、调用service,返回值PageResult
        PageResult vo = usersService.blacklist(page,pagesize);
        //2、返回值
        return ResponseEntity.ok(vo);
    }


    /**
     * 移除黑名单数据
     *    DELETE   /blacklist/{uid}
     *    请求参数
     *       uid：路径参数
     *          /blacklist/6
     *    blackUserId:黑名单用户id
     */
    @DeleteMapping("/blacklist/{blackUserId}")
    public ResponseEntity blacklist(@PathVariable("blackUserId") Long blackUserId) {
        //调用service
        usersService.delBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询互相喜欢、喜欢、粉丝、谁看过我
     *    GET    /friends/:type
     *    路径参数：type
     *       1 互相关注
     *       2 我关注
     *       3 粉丝
     *       4 谁看过我
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable("type") Integer type,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult vo = usersService.friends(type,page,pagesize);
        return ResponseEntity.ok(vo);
    }
}
